<template>
    <div>
        <div class="i-table-no-border">
            <Table
                highlight-row
                ref="table"
                :columns="columns"
                :data="list"
                :loading="loading"
                size="small"
                :max-height="500"
                @on-filter-change="handleFilterChange"
                @on-sort-change="sortchange"
            >
                <template slot-scope="{ row }" slot="info">
                    <a @click="handleDetailPatient(row)"
                    ><Icon
                        type="ios-water"
                        v-if="+row.is_share === 1"
                    ></Icon>{{ row.true_name || row.user_name }}
                        {{ row.gender_str }} {{ row.age }}</a
                        >
                </template>
                <template slot-scope="{ row }" slot="state">
                    <Tag v-if="row.status_name === '失效'" color="blue">{{
                        row.status_name
                    }}</Tag>
                    <Tag
                        v-else-if="row.status_name === '跟进中'"
                        color="volcano"
                    >{{ row.status_name }}</Tag
                    >
                    <Tag
                        v-else-if="row.status_name === '预约'"
                        color="cyan"
                    >{{ row.status_name }}</Tag
                    >
                    <Tag
                        v-else-if="row.status_name === '到诊'"
                        color="green"
                    >{{ row.status_name }}</Tag
                    >
                    <Tag
                        v-else-if="row.status_name === '已到老患者'"
                        color="orange"
                    >{{ row.status_name }}</Tag
                    >
                    <Tag v-else-if="row.status_name" color="orange">{{
                        row.status_name
                    }}</Tag>
                </template>
                <template slot-scope="{ row }" slot="label">
                    <Poptip
                        word-wrap
                        trigger="hover"
                        placement="right"
                        width="400"
                    >
                        <template v-for="(item, index) in row.tags_name">
                            <Tag v-show="index<3" :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                        </template>
                        <div slot="content">
                            <template v-for="(item, index) in row.tags_name">
                                <Tag :color="item.type==1?'warning':'primary'" :key="index">{{ item.tags_name.replace(/[\r\n]/g, "") }}</Tag>
                            </template>
                        </div>
                    </Poptip>
                </template>
                <template slot-scope="{ row }" slot="labels">
                    <Poptip
                        word-wrap
                        trigger="hover"
                        placement="right"
                        maxWidth="400"
                    >
                        <Tag
                            color="default"
                            v-for="(item, index) in row.body_parts"
                            :key="index"
                            v-if="index < 3"
                        >{{ item.replace(/[\r\n]/g, "") }}</Tag
                        >
                        <div slot="content">
                            <Tag
                                color="default"
                                v-for="(item, index) in row.body_parts"
                                :key="index"
                            >{{ item.replace(/[\r\n]/g, "") }}</Tag
                            >
                        </div>
                    </Poptip>
                    <!-- <Tooltip content="Bottom Center text" placement="bottom">
                        <Tag v-if="+index <= 2" v-for="(item, index) in row.body_parts" :key="index">{{ item }}
                        </Tag>
                        <Tag slot="content" v-for="(item, index) in row.body_parts" :key="index">{{ item }}</Tag>
                    </Tooltip> -->
                </template>
                <template slot-scope="{ row }" slot="disease">
                    <span>{{
                        row.child_diseases_name || row.diseases_name
                    }}</span>
                </template>
                <template slot-scope="{ row }" slot="channel">
                    <Badge
                        v-if="row.source_name === 0"
                        status="success"
                        :text="row.source_name"
                    ></Badge>
                    <Badge
                        v-if="row.source_name === 1"
                        status="processing"
                        :text="row.source_name"
                    ></Badge>
                    <Badge
                        v-if="row.source_name === 2"
                        status="error"
                        :text="row.source_name"
                    ></Badge>
                    <Badge
                        v-if="row.source_name === 3"
                        status="warning"
                        :text="row.source_name"
                    ></Badge>
                    <span>{{ row.source_name }}</span>
                </template>
                <template slot-scope="{ row, index }" slot="actions">
                    <a
                        v-hasC="{
                            name: 'v1-patient-index-edit-info2',
                            flag: '/v1/patient/index/edit-info',
                        }"
                        @click="handleOpenDrawer(row)"
                    >编辑</a
                    >
                </template>
            </Table>
            <div
                class="ivu-mt iuv-text-center"
                slot="footer"
                style="text-align: center;"
            >
                <Page
                    show-total
                    show-elevator
                    show-sizer
                    :total="total"
                    :current.sync="params.page"
                    :page-size="params.pageSize"
                    :page-size-opts="pageOpts"
                    @on-change="changepage"
                    @on-page-size-change="changepageSize"
                ></Page>
            </div>
        </div>
        <patient-detail
            ref="patientDetail"
            :btnShow="btnShow"
            @cancelDrawer="askData"
            @askData="askData"
        ></patient-detail>
        <edit-patient
            ref="editPatient"
            @patientfun="askData"
            v-if="!loading"
        ></edit-patient>
        <!-- <return-visit ref="returnVisit" /> -->
        <return-visit ref="returnVisit" @patientfuns="askData" ></return-visit>
        <copymodal ref="copymodal" @askData="askData" :btnShow="btnShows"></copymodal>
        <FooterToolbar>
            <div
                style="width:100vw;
            height:56px;display:flex;
            justify-content: center;
            align-items: center"
            >
                <Button
                    v-hasC="{
                        name: 'patient-index-create',
                        flag: 'v1/patient/index/create',
                    }"
                    type="primary"
                    class="v-mt-20"
                    size="large"
                    :loading="addloading"
                    @click="handleOpenDrawer(-1)"
                >新增</Button
                >
            </div>
        </FooterToolbar>
    </div>
</template>
<script>
    import patientDetail from '@/components/patientDetail';
    import editPatient from '@/components/editPatient';
    import returnVisit from '@/components/returnVisit';
    import copymodal from './copymodal';
    import { patientindexcancelarrive, patientindexdelete } from '@/api/hqapi';
    export default {
        name: 'tablelist',
        props: ['params', 'columns', 'list', 'loading', 'total', 'currentTab'],
        components: { patientDetail, editPatient, returnVisit, copymodal },
        data() {
            return {
                btnShows: {
                    name: 'v1-patient-index-delete4',
                    flag: 'v1/patient/index/delete'
                },
                pageOpts: [10, 15, 20, 30, 50], // 分页每页数量
                btnShow: [
                    {
                        name: 'patient-index-sub5',
                        flag: 'v1/patient/index/sub'
                    },
                    {
                        name: 'patient-index-edit-remark5',
                        flag: 'v1/patient/index/edit-remark'
                    },
                    {
                        name: 'v1-patient-index-update',
                        flag: 'v1/patient/index/update'
                    },
                    {
                        name: 'patient-callback-callback-common-list4',
                        flag: 'v1/patient/callback/callback-common-list'
                    },
                    {
                        name: 'patient-index-edit-tags10',
                        flag: 'v1/patient/index/edit-tags'
                    },
                    {
                        name: 'patient-index-edit-tags13',
                        flag: 'v1/patient/index/edit-tags'
                    }
                ],
                filteredValue: [],
                filterType: undefined,
                addloading: false
            };
        },
        methods: {
            initial() {
            },
            askData() {
                this.$emit('askData', this.params);
            },
            handleDetailPatient(row) {
                if(this.currentTab === 'add' && +row.patient_data_type !== 0) {
                    this.$refs.patientDetail.handleOpen(row, true); 
                }else this.$refs.patientDetail.handleOpen(row);
            },
            handleOpenDrawer(row) {
                if(row === -1) {
                    this.addloading = true;
                    setTimeout(() => {
                        this.addloading = false;
                    }, 500);
                    this.$refs.editPatient.handleOpenDrawer();
                }else {
                    if(this.currentTab === 'add' && +row.patient_data_type !== 0) {
                        this.$refs.editPatient.handleEdit(row, true); 
                    }else this.$refs.editPatient.handleEdit(row);
                }
            },
            delpatient(row, index) {
                this.$Modal.confirm({
                    title: '删除确认',
                    content: '<p>删除后患者进入回收站，将不在其他列表展示</p>',
                    loading: true,
                    onOk: () => {
                        patientindexdelete({ id: row.id }).then(() => {
                            this.$Message.info('删除成功');
                            this.askData();
                            this.$Modal.remove();
                            this.data.splice(index, 1);
                        });
                    }
                });
            },
            handleFilterChange(value) {
                this.params.not_callback_start = '';
                this.params.not_callback_end = '';
                this.filteredValue = [];
                if(+value._filterChecked[0] === 1) {
                    this.filteredValue = ['1'];
                    this.params.not_callback_start = this.$Date().subtract(10, 'day').format('YYYY-MM-DD');
                    this.params.not_callback_end = this.$Date().format('YYYY-MM-DD');
                }else if(+value._filterChecked[0] === 2) {
                    this.filteredValue = ['2'];
                    this.params.not_callback_start = this.$Date().subtract(20, 'day').format('YYYY-MM-DD');
                    this.params.not_callback_end = this.$Date().subtract(11, 'day').format('YYYY-MM-DD');
                }else if(+value._filterChecked[0] === 3) {
                    this.filteredValue = ['3'];
                    this.params.not_callback_start = this.$Date().subtract(30, 'day').format('YYYY-MM-DD');
                    this.params.not_callback_end = this.$Date().subtract(21, 'day').format('YYYY-MM-DD');
                }else if(+value._filterChecked[0] === 4) {
                    this.filteredValue = ['4'];
                    this.params.not_callback_start = this.$Date().subtract(60, 'day').format('YYYY-MM-DD');
                    this.params.not_callback_end = this.$Date().subtract(31, 'day').format('YYYY-MM-DD');
                }else if(+value._filterChecked[0] === 5) {
                    this.filteredValue = ['5'];
                    this.params.not_callback_start = '';
                    this.params.not_callback_end = this.$Date().subtract(61, 'day').format('YYYY-MM-DD');
                }else {
                    this.params.not_callback_start = '';
                    this.params.not_callback_end = '';
                }
                for(let item of this.columns) {
                    if(item.title === '未访天数') {
                        item.filteredValue = this.filteredValue;
                    }
                }
                this.$emit('askData', this.params);
            },
            patientfun() {
                this.$emit('askData', this.params);
            },
            /* @函数功能: 分页 */
            changepage(e) {
                this.params.page = e;
                this.$emit('askData', this.params);
            },
            /* @函数功能: 每页数量 */
            changepageSize(e) {
                this.params.pageSize = e;
                this.$emit('askData', this.params);
            },
            sortchange(e) {
                if(e.key === 'appointment_time_str') {
                    if(e.order === 'desc') {
                        this.params.sort_appointment_time = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_appointment_time = 1;
                    }else {
                        this.params.sort_appointment_time = 0;
                    }
                    this.params.sort_last_callback_time = 0;
                    this.params.sort_callback_count = 0;
                    this.params.sort_revisiting_time = 0;
                    this.params.sort_created_at = 0;
                }else if(e.key === 'callback_count') {
                    if(e.order === 'desc') {
                        this.params.sort_callback_count = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_callback_count = 1;
                    }else {
                        this.params.sort_callback_count = 0;
                    }
                    this.params.sort_last_callback_time = 0;
                    this.params.sort_appointment_time = 0;
                    this.params.sort_revisiting_time = 0;
                    this.params.sort_created_at = 0;
                }else if(e.key === 'revisiting_time_str') {
                    if(e.order === 'desc') {
                        this.params.sort_revisiting_time = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_revisiting_time = 1;
                    }else {
                        this.params.sort_revisiting_time = 0;
                    }
                    this.params.sort_last_callback_time = 0;
                    this.params.sort_appointment_time = 0;
                    this.params.sort_callback_count = 0;
                    this.params.sort_created_at = 0;
                }else if(e.key === 'uncallback_day') {
                    for(let item of this.columns) {
                        if(item.title === '未访天数') {
                            item.filteredValue = [];
                        }
                    }
                    if(e.order === 'desc') {
                        this.params.sort_last_callback_time = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_last_callback_time = 1;
                    }else {
                        this.params.sort_last_callback_time = 0;
                    }
                    this.params.sort_revisiting_time = 0;
                    this.params.sort_appointment_time = 0;
                    this.params.sort_callback_count = 0;
                    this.params.sort_created_at = 0;
                }else if(e.key === 'created_at_str') {
                    if(e.order === 'desc') {
                        this.params.sort_created_at = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_created_at = 1;
                    }else {
                        this.params.sort_created_at = 0;
                    }
                    this.params.sort_revisiting_time = 0;
                    this.params.sort_appointment_time = 0;
                    this.params.sort_callback_count = 0;
                    this.params.sort_last_callback_time = 0;
                }else if(e.key === 'belong_assign_time_day') {
                    if(e.order === 'desc') {
                        this.params.sort_belong_assign_time = 2;
                    }else if(e.order === 'asc') {
                        this.params.sort_belong_assign_time = 1;
                    }else {
                        this.params.sort_belong_assign_time = 0;
                    }
                    this.params.sort_revisiting_time = 0;
                    this.params.sort_appointment_time = 0;
                    this.params.sort_callback_count = 0;
                    this.params.sort_last_callback_time = 0;
                }
                this.params.page = 1;
                this.$emit('askData', this.params);
            }
        }
    };
</script>
